#include "stdafx.h"
#include "MyWebBrowser.h"
#include <Windows.h>

#ifndef CPP_WEB
class IWebControl
{
public:
	virtual void __stdcall SetWidth(int width)=0;
	virtual void __stdcall SetHeight(int height)=0;
	virtual void __stdcall SetLeft(int left)=0;
	virtual void __stdcall SetTop(int top)=0;
	virtual void __stdcall SetParentWnd(HWND width)=0;
	virtual void __stdcall Navigate(const char* szUrl)=0;
	virtual void __stdcall Release()=0;
};

typedef IWebControl* (__stdcall *CreateWebControlFunc)();
static CreateWebControlFunc CreateWebControl;

class WebDllLoader
{
public:
	WebDllLoader()
	{
		mDllHandle = LoadLibrary(_T("webdll.dll"));
		CreateWebControl = (CreateWebControlFunc)GetProcAddress(mDllHandle, "CreateWebControl");
		CreateWebControl();
	}
private:
	HMODULE mDllHandle;	
};

static WebDllLoader sWebDllLoader;
#endif

BOOL CMyWebBrowser::Create(LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd * pParentWnd, 
	UINT nID, CFile * pPersist, BOOL bStorage, BSTR bstrLicKey)
{
#ifdef CPP_WEB
	return CreateControl(GetClsid(),lpszWindowName,dwStyle,rect,pParentWnd,nID);
#else
	mWebControl = CreateWebControl();
	mWebControl->SetWidth(rect.right);
	mWebControl->SetHeight(rect.bottom);
	mWebControl->SetParentWnd(m_hWnd);
	return TRUE;
#endif
}

BOOL CMyWebBrowser::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle,
	const RECT& rect, CWnd * pParentWnd, UINT nID, CCreateContext * pContext)
{
#ifdef CPP_WEB
	return CreateControl(GetClsid(),lpszWindowName,dwStyle,rect,pParentWnd,nID,pPersist,bStorage,bstrLicKey);
#else
	mWebControl = CreateWebControl();
	mWebControl->SetWidth(rect.right);
	mWebControl->SetHeight(rect.bottom);
	BOOL boSuccess = CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
	if(boSuccess) 
	{
		mWebControl->SetParentWnd(m_hWnd);
	}
	return boSuccess;
#endif
}

void CMyWebBrowser::Navigate(const char* szUrl)
{
#ifdef CPP_WEB
#else
	mWebControl->Navigate(szUrl);
#endif
}